package com.sbio.udiapp.util;


import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;

/**
 * RSA加密解密
 * 
 * @author ruoyi
 **/
public class RsaUtils
{
    // Rsa 私钥
    public static String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCu3z/LX6EIHBiU" +
            "bXvVbq/m2vBefOp8QLNWBiQowLTZhrTbm8BAwgNuYwtj5rNSaNhM6eYF04Dso2GU" +
            "w5V5EoPRnyypOX/lC3Lf8FpKj5ffBMZS1d1MKeNicBNHFlJFSJRZFedCSVPhJ8IO" +
            "ukQwN0tNYBtTCvP9VdGdWAyMsWogaxgaHv33VTW3enJNm8ud0fbaw6TNzAXpqbFA" +
            "30wCMpH+zWCLSD9sCvepJp/uxDdMoZ4FgtTg59hj7kp/h2lf6z5POPCVW32Sa6t6" +
            "xkfCvxDwMsWZyuN2CUqOVRlj7Qa/1/U0h1zekgR6/w6+GVVwocyREmbGCfzvK5tC" +
            "jjjFg245AgMBAAECggEBAKNVmIN/zawCeh7g6CJhYWESLCNFL8tgr2DxGYMLA6CN" +
            "xo28u2SBEq1fS3tDN0kI7sdgTN4yQCBHNlS6HfnZBlCcCR4Cu+eWuvRZziwIVQUU" +
            "s50Zbrq+AUmnfwoN2kgmU5Gl+LwcO4F5HjOdXSVf8DDUTJFTELQWHVSGsioTaBng" +
            "Ac/jdp1M2yW05UBMEwMOOU5QDhOJNR8Ko8bb10DcG9MU+rwueNmH0OZrED74rH25" +
            "DsxN8JxdhAng45nx+Q24SRUlRBsRZWMiYo9II8KZLUTjnaJFFWJAYRNwyjQk6MIl" +
            "spF49YgRHFjyHyvhP64T+DdwPno2OItVNbOHJN/CVQECgYEA4IA9tecvcp+RP2z3" +
            "L3SaTcnCICLAdVa4531VCWKRbmcOooZ7U0JFm+2eYT5KApTTUJhnxTwaYsUXkSLT" +
            "92yJ3A/xR1KCAIu6XLAvIKRL/euyb7sC8qBeAz61PDGcmk7duLhNcrUoXA4lWKwE" +
            "vCVtd+Q8C9V/6E2wmdJITm57/dkCgYEAx2homJQFMJuxh96xWgQd6v/cprdrO2zO" +
            "CihypLMcx86YKblvBGCrxkZ0NsSud8oFWGYH2Uu9WjjFMd7bzocA6mpHbQmIErxC" +
            "neOgOC7eZ5voMjbHNMajt0aEMUUam8dKUiKn1VJ5aQQD67xNr8I8wanjEsq/MLlT" +
            "9QCF+9ev12ECgYEAp71J3osQ+ZzeArzg6BVAYZS5NhehI0sbtuL/UZbTQOFBnw/K" +
            "P4hJyFq/trq6w7j9Nr/Y8RaaRGgs1qVmKP1w9/r1dSvhtJSyNyTw4XvAx8V+ulrJ" +
            "tg06kUQeWA5dizIYGjeirm1K7ZUoNFu64fU5GJZwgwsPmbvKkvP6UJfejukCgYBe" +
            "IZpmlFFS3dxTUDuWS6U33upxRNp45ebpgqiDuQ2YAJ1x7NhMT7om0fFgPT5Cy/nz" +
            "/c4stIVBRtVHsnU+rBoxjlQDU+GJPpeP4vkqLG3OqRlWQueMsUxtYvtaKs3hxZT+" +
            "6t8hY1puyXd0W6KsmluF7EWGBHF2dbX9TO7ysKGQYQKBgQCAwIisHMndbP8wDvNZ" +
            "LV/YcHBhmMkqMhZXIWBh2fe5uHN0LCP92MOQuGgqA+jvSVPnlSS65HDgJCzmlBOe" +
            "2iTcM2fMi4PaO3IxzR2QcpG87Oq+vFuiFcjZNDz7v7cMsDi2HBnjH0Wc6iQu42GX" +
            "fIt40AeZ0qcWuogSjkZ5Yv1UCw==";

    /**
     * 私钥解密
     *
     *
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) throws Exception
    {
        return decryptByPrivateKey(privateKey, text);
    }

    /**
     * 公钥解密
     *
     * @param publicKeyString 公钥
     * @param text 待解密的信息
     * @return 解密后的文本
     */
    public static String decryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(Base64.getDecoder().decode(text));
        return new String(result);
    }

    /**
     * 私钥加密
     *
     * @param privateKeyString 私钥
     * @param text 待加密的信息
     * @return 加密后的文本
     */
    public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.getEncoder().encodeToString(result);
    }

    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.getDecoder().decode(text));
        return new String(result);
    }

    /**
     * 公钥加密
     *
     * @param publicKeyString 公钥
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.getEncoder().encodeToString(result);
    }



    /**
     * 构建RSA密钥对
     *
     * @return 生成后的公私钥信息
     */
    public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException
    {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded());
        String privateKeyString = Base64.getEncoder().encodeToString(rsaPrivateKey.getEncoded());
        return new RsaKeyPair(publicKeyString, privateKeyString);
    }

    /**
     * RSA密钥对对象
     */
    public static class RsaKeyPair
    {
        private final String publicKey;
        private final String privateKey;

        public RsaKeyPair(String publicKey, String privateKey)
        {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }

        public String getPublicKey()
        {
            return publicKey;
        }

        public String getPrivateKey()
        {
            return privateKey;
        }
    }
}
